package com.jhsw.roadsun.module.business.controller.admin.useraddress;

import com.jhsw.roadsun.framework.apilog.core.annotation.ApiAccessLog;
import com.jhsw.roadsun.framework.common.pojo.CommonResult;
import com.jhsw.roadsun.framework.common.pojo.PageParam;
import com.jhsw.roadsun.framework.common.pojo.PageResult;
import com.jhsw.roadsun.framework.common.util.object.BeanUtils;
import com.jhsw.roadsun.framework.excel.core.util.ExcelUtils;
import com.jhsw.roadsun.module.business.controller.admin.useraddress.vo.UserAddressPageReqVO;
import com.jhsw.roadsun.module.business.controller.admin.useraddress.vo.UserAddressRespVO;
import com.jhsw.roadsun.module.business.dal.dataobject.useraddress.UserAddressDO;
import com.jhsw.roadsun.module.business.service.useraddress.UserAddressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.jhsw.roadsun.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.jhsw.roadsun.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 地址簿")
@RestController
@RequestMapping("/business/user-address")
@Validated
public class UserAddressController {

    @Resource
    private UserAddressService userAddressService;

    @DeleteMapping("/delete")
    @Operation(summary = "删除地址簿")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:user-address:delete')")
    public CommonResult<Boolean> deleteUserAddress(@RequestParam("id") Long id) {
        userAddressService.deleteUserAddress(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得地址簿")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:user-address:query')")
    public CommonResult<UserAddressRespVO> getUserAddress(@RequestParam("id") Long id) {
        UserAddressDO userAddress = userAddressService.getUserAddress(id);
        return success(BeanUtils.toBean(userAddress, UserAddressRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得地址簿分页")
    @PreAuthorize("@ss.hasPermission('business:user-address:query')")
    public CommonResult<PageResult<UserAddressRespVO>> getUserAddressPage(@Valid UserAddressPageReqVO pageReqVO) {
        PageResult<UserAddressDO> pageResult = userAddressService.getUserAddressPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserAddressRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出地址簿 Excel")
    @PreAuthorize("@ss.hasPermission('business:user-address:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserAddressExcel(@Valid UserAddressPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserAddressDO> list = userAddressService.getUserAddressPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "地址簿.xls", "数据", UserAddressRespVO.class,
                        BeanUtils.toBean(list, UserAddressRespVO.class));
    }

}